package org.telegram.bot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.enums.StatusEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.telegram.bot.domain.BotChatWelcome;
import org.telegram.bot.domain.BotKeyboard;
import org.telegram.bot.handle.IBotSendMessage;
import org.telegram.bot.mapper.BotChatWelcomeMapper;
import org.telegram.bot.service.IBotChatWelcomeService;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.MessageEntity;
import org.telegram.telegrambots.meta.api.objects.User;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 入群欢迎Service业务层处理
 *
 * @author wyx
 * @date 2024-11-23
 */
@Slf4j
@Service
public class BotChatWelcomeServiceImpl implements IBotChatWelcomeService, IBotSendMessage
{
    @Resource
    private BotChatWelcomeMapper botChatWelcomeMapper;

    /**
     * 查询入群欢迎
     *
     * @param id 入群欢迎主键
     * @return 入群欢迎
     */
    @Override
    public BotChatWelcome selectBotChatWelcomeById(Long id)
    {
        BotChatWelcome bean = botChatWelcomeMapper.selectOne(new LambdaQueryWrapper<BotChatWelcome>().eq(BotChatWelcome::getChatId, id));
        if(bean==null){
            return botChatWelcomeMapper.selectById(id);
        }
        return bean;
    }
    
    /**
     * 查询入群欢迎列表
     *
     * @param botChatWelcome 入群欢迎
     * @return 入群欢迎
     */
    @Override
    public List<BotChatWelcome> selectBotChatWelcomeList(BotChatWelcome botChatWelcome)
    {
        return botChatWelcomeMapper.selectBotChatWelcomeList(botChatWelcome);
    }

    /**
     * 新增入群欢迎
     *
     * @param botChatWelcome 入群欢迎
     * @return 结果
     */
    @Override
    public int insertBotChatWelcome(BotChatWelcome botChatWelcome)
    {
        return botChatWelcomeMapper.insert(botChatWelcome);
    }

    /**
     * 修改入群欢迎
     *
     * @param botChatWelcome 入群欢迎
     * @return 结果
     */
    @Override
    public int updateBotChatWelcome(BotChatWelcome botChatWelcome)
    {
        if(botChatWelcome.getId()==null && botChatWelcome.getChatId()!=null){
            BotChatWelcome bean = selectBotChatWelcomeById(botChatWelcome.getChatId());
            if(bean==null){
                return botChatWelcomeMapper.insert(botChatWelcome);
            }
            botChatWelcome.setId(bean.getId());
        }
        return botChatWelcomeMapper.updateById(botChatWelcome);
    }

    /**
     * 批量删除入群欢迎
     *
     * @param ids 需要删除的入群欢迎主键
     * @return 结果
     */
    @Override
    public int deleteBotChatWelcomeByIds(Long[] ids)
    {
        return botChatWelcomeMapper.deleteByIds(Arrays.asList(ids));
    }

    /**
     * 删除入群欢迎信息
     *
     * @param id 入群欢迎主键
     * @return 结果
     */
    @Override
    public int deleteBotChatWelcomeById(Long id)
    {
        return botChatWelcomeMapper.deleteById(id);
    }
    
    /**
     * 发送欢迎语
     * @param botId
     * @param chatId
     * @param user
     */
    @Override
    public void sendWelcome(String botId, Long chatId,User user){
        BotChatWelcome botChatWelcome = selectBotChatWelcomeById(chatId);
        if(botChatWelcome!=null && StatusEnum.OK.getCode().equals(botChatWelcome.getStatus())){
            BotKeyboard keyboard = BeanUtil.toBean(botChatWelcome,BotKeyboard.class);
            List<MessageEntity> entities = new ArrayList<>();
            //@人存原生user
            MessageEntity messageEntity = MessageEntity.builder()
                    .user(user)
                    .type("text_mention")
                    .offset(0)
                    .length(user.getId().toString().length()+1)
                    .build();
            entities.add(messageEntity);
            send(botId,SendMessage.builder().chatId(chatId).text("@"+user.getId()+"\n"+keyboard.getContent()).replyMarkup(keyboard.getInlineKeyboardMarkup()).entities(entities).build());
        }
    }
}
